﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ControllerManageServer.Packet
{
    /// <summary>
    /// 爱鸥指令数据模型
    /// </summary>
    internal class AioiActionPacketModel
    {
        /// <summary>
        /// 爱鸥指令集
        /// </summary>
        private string[] AioiActionCollection = new string[] { "@", "Z", "Z1", "B1", "M", "Am", "FAm", "PP1", "PP5", "PP506", "FP1", "FP5", "P1", "P4", "P5", "Pm", "L", "P6", "FP6", "FPm", "PQ1", "PQ2", "PQ5", "D", "X", "Q2", "XA", "T", "A", "Az", "Ac", "G", "Ar", "J", "E", "Ai", "P2", "FP2", "PP2", "t", "o", "n", "n1", "a" };
        /// <summary>
        /// 连番号
        /// </summary>
        public int SerialNo { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public bool State { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 命令类型
        /// </summary>
        public string CmdType { get; set; }
        /// <summary>
        /// 数据报文
        /// </summary>
        public byte[] DataBuffer { get; set; }

        public AioiActionPacketModel() { }

        public void Unserialize(string iTemp)
        {
            SerialNo = int.Parse(iTemp.Substring(0, 3));// 接收连番号
            int len = int.Parse(iTemp.Substring(3, 4));// 数据指令长度
            CmdType = null;
            if (len > 4) CmdType = iTemp.Substring(7, 5);// 指令类型
            if (CmdType != null && AioiActionCollection.Contains(CmdType))
            {
                // 五位的指令处理
                return;
            }
            if (len > 2) CmdType = iTemp.Substring(7, 3);
            if (CmdType != null && AioiActionCollection.Contains(CmdType))
            {
                // 三位的指令处理
                if ("PP5".Equals(CmdType))
                {
                    // 找到m4后向后移4位(m4两个字节+m4后面跟着的数据是两个字节)就是地址
                    Address = iTemp.Substring(iTemp.IndexOf("m4") + 4 - 1, 4);
                }
                else if ("PQ1".Equals(CmdType))
                {
                    Address = iTemp.Substring(iTemp.IndexOf("AF101") + 4, 4);
                }
                else if ("FP1".Equals(CmdType)) {
                    Address = iTemp.Substring(iTemp.IndexOf("FP1") + 9, 4);
                }
                return;
            }
            if (len > 1) CmdType = iTemp.Substring(7, 2);
            if (CmdType != null && AioiActionCollection.Contains(CmdType))
            {
                // 两位的指令处理
                if ("Az".Equals(CmdType))
                {
                    Address = iTemp.Substring(9, 4);// 数据地址
                }
                return;
            }
            CmdType = iTemp.Substring(7, 1);
            if ((CmdType.Equals("a") || CmdType.Equals("t") || CmdType.Equals("D")) && len > 4)
            {
                Address = iTemp.Substring(8, 4);// 数据地址
                State = true;// 能收到地址就是成功
            }
        }
    }
}
